package com.jindu.producer.system.controller;

import com.jindu.kernel.page.AlphaPage;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.system.jpa.DictionaryJpa;
import com.jindu.producer.system.model.SDictionaryChildren;
import com.jindu.producer.system.model.SDictionaryEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/3/21
 */
@Api(description = "系统数据字典管理", tags = "DictionaryController", basePath = "/dictionary")
@RestController
@RequestMapping(value = "/dictionary",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DictionaryController {

    @Autowired
    DictionaryJpa dictionaryJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据字典ID，查询字典", notes = "返回单个字典的信息,字典详细信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<SDictionaryEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(dictionaryJpa.findById(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据字典的父ID，查询字典列表", notes = "返回某节点字典下所有子字典的列表")
    @RequestMapping(value = "getByParentId",method = RequestMethod.GET)
    public JsonResult<AlphaPage<SDictionaryEntity>> getByParentId(@RequestParam String parentId,
                                                                   @RequestParam(value = "pageNo",defaultValue = "0") Integer pageNo,
                                                                   @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        Sort sort = new Sort(Sort.Direction.DESC, "sortId");
        return new AlphaPage<>(dictionaryJpa.findAllByParentId(parentId,new PageRequest(pageNo, pageSize, sort))).toJsonResult();
    }

    @RequestMapping(value = "getListByParentId",method = RequestMethod.GET)
    public List<SDictionaryEntity> getListByParentId(@RequestParam(value = "parentId") String parentId){
        return dictionaryJpa.findAllByParentIdOrderBySortId(parentId);
    }

    @ApiOperation(value = "根据字典的父ID，查询字典包含的子元素的集合。主要用在地区级联查询", notes = "返回某节点字典下所有子字典的json，并且包含子元素")
    @RequestMapping(value = "getListChildren",method = RequestMethod.GET)
    public JsonResult<List<SDictionaryChildren>> getListChildren(@RequestParam String parentId){

        List<SDictionaryChildren> entity=new ArrayList<>();
        List<SDictionaryEntity> one=dictionaryJpa.findAllByParentIdOrderBySortId(parentId);
        SDictionaryEntity nodeTwoParent=dictionaryJpa.findAllByValue(parentId);
        List<SDictionaryEntity> nodeTwoList=dictionaryJpa.findAllByParentIdOrderBySortId(nodeTwoParent.getId());

        for (SDictionaryEntity root : one) {

            SDictionaryChildren nodeOne=new SDictionaryChildren();

            nodeOne.setId(root.getId());
            nodeOne.setSortId(root.getSortId());
            nodeOne.setParentId(root.getParentId());
            nodeOne.setLabel(root.getName());
            nodeOne.setValue(root.getValue());

            List<SDictionaryChildren> nodeOneChild=new ArrayList<>();

            for (SDictionaryEntity two :nodeTwoList) {

                if(root.getSortId()==two.getSortId()){

                    SDictionaryChildren children=new SDictionaryChildren();

                    children.setId(two.getId());
                    children.setSortId(two.getSortId());
                    children.setParentId(two.getParentId());
                    children.setLabel(two.getName());
                    children.setValue(two.getValue());

                    nodeOneChild.add(children);
                }
            }
            nodeOne.setChildren(nodeOneChild);
            entity.add(nodeOne);
        }

        return JsonResult.getSuccessResult(entity);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存字典信息", notes = "保存字典信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<SDictionaryEntity> save(@RequestBody SDictionaryEntity dictionaryEntity){

        SDictionaryEntity sDictionaryEntity=dictionaryJpa.findById(dictionaryEntity.getId());
        if(sDictionaryEntity==null){
            dictionaryEntity.setId(IdGenerator.uuid());
            dictionaryEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            dictionaryEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(dictionaryJpa.saveAndFlush(dictionaryEntity));
        }else {
            dictionaryEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(dictionaryEntity,sDictionaryEntity, AlphaBeanUtils.getNullPropertyNames(dictionaryEntity));
            return JsonResult.getSuccessResult(dictionaryJpa.saveAndFlush(sDictionaryEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除标签的信息", notes = "根据标签的ID,删除标签数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(dictionaryJpa.deleteById(id));
    }


}
